package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.common.MD5Utils;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.utils.RegularUtils;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.HSPassedwordEditText;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.InitializePresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.view.base.InitializeView;

import org.json.JSONException;
import org.json.JSONObject;

import de.greenrobot.event.EventBus;


/**
 * @Title: SetUpdateLoginPwdActivity.java 
 * @Description: 设置-修改登录密码
 * @date 2016-1-8 下午4:45:30 
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com 
 * @replace author:
 * @replace date:
 * @version V1.0   
 */
public class SetUpdateLoginPwdActivity extends BaseActivity implements OnClickListener,InitializeView {

	
	/**
	 * 提交按钮
	 */
	private HSButton btn_submit=null;

	/**忘记密码按钮**/
//	private ClickableTextView tv_forgetpwd=null;
	/**原密码输入框**/
	private HSPassedwordEditText et_originalpwd=null;
	/**新密码输入框**/
	private HSPassedwordEditText et_newpwd=null;
	/**确认密码输入框**/
	private HSPassedwordEditText et_newpwdagain=null;
	
	private RequestPresenter mPresenter=null;
	@Override
	protected boolean isApplyKitKatTranslucency() {
		return true;
	}

	@Override
	protected void getBundleExtras(Bundle extras) {
	}

	@Override
	protected int getContentViewLayoutID() {
		return R.layout.act_set_update_loginpwd;
	}

	@Override
	protected void onEveComming(EventCenter eventCenter) {

	}

	@Override
	protected boolean isBindEventBusHere() {
		return false;
	}

	@Override
	protected View getLoadingTargetView() {
		return null;
	}

	@Override
	protected void initViewsAndEvents() {
		setToolbarTitle(R.string.update_pwd);

		
		mPresenter=new InitializePresenterImpl(this, this);
		btn_submit = findView(R.id.btn_submit);
		btn_submit.setOnClickListener(this);

//		tv_forgetpwd= findView(R.id.tv_forgetpwd);
//		tv_forgetpwd.setOnClickListener(this);
		
		et_originalpwd=findView(R.id.et_originalpwd);
		et_newpwd=findView(R.id.et_newpwd);
		et_newpwdagain=findView(R.id.et_newpwdagain);
		
//		et_originalpwd.setKeyListener(new DigitsKeyListener(){
//			@Override
//			public int getInputType() {
//				return InputType.TYPE_TEXT_VARIATION_PASSWORD;
//			}
//			
//			@Override
//            protected char[] getAcceptedChars() {
//                char[] data = mContext.getString(R.string.input_digitalANDletter).toCharArray();   
//                return data;
//            }o
//		});
//		
//		et_newpwd.setKeyListener(new DigitsKeyListener(){
//			@Override
//			public int getInputType() {
//				return InputType.TYPE_TEXT_VARIATION_PASSWORD;
//			}
//			
//			@Override
//            protected char[] getAcceptedChars() {
//                char[] data = mContext.getString(R.string.input_digitalANDletter).toCharArray();   
//                return data;
//            }
//		});
//		
//		et_newpwdagain.setKeyListener(new DigitsKeyListener(){
//			@Override
//			public int getInputType() {
//				return InputType.TYPE_TEXT_VARIATION_PASSWORD;
//			}
//			
//			@Override
//            protected char[] getAcceptedChars() {
//                char[] data = mContext.getString(R.string.input_digitalANDletter).toCharArray();   
//                return data;
//            }
//		});
	}


	protected void initImmersionBar() {
		super.initImmersionBar();
		mImmersionBar.titleBar(R.id.common_toolbar).init();
	}


	@Override
	protected void onNetworkConnected(NetType type) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onNetworkDisConnected() {
		// TODO Auto-generated method stub

	}

	@Override
	protected boolean isApplyStatusBarTranslucency() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected boolean toggleOverridePendingTransition() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected TransitionMode getOverridePendingTransitionMode() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_submit:
			// TODO 处理修改请求
			validate();
			break;
//		case R.id.tv_forgetpwd:
//			readyGo(ForgetPasswordInputPhoneActivity.class);
//			break;
		default:
			break;
		}

	}
	
	/**
	 * 验证
	 * @param
	 */
	private void validate(){
		//判断是否为空
		if(TextUtils.isEmpty(et_originalpwd.getText())){
			showToast(R.string.input_original_pwd);
			return;
		}
		//判断是否为空
		if(TextUtils.isEmpty(et_newpwd.getText())){
			showToast(R.string.input_new_pwd);
			return;
		}
		//判断是否为空
		if(TextUtils.isEmpty(et_newpwdagain.getText())){
			showToast(R.string.input_new_pwd_again);
			return;
		}
		//判断是否一致
		if(!TextUtils.equals(et_newpwd.getText().toString(), et_newpwdagain.getText().toString())){
			showToast(R.string.input_pwd_same);
			return;
		}
		//判断格式是否正确
		if(!RegularUtils.regular(RegularUtils.REGEX_PWD, et_originalpwd.getText().toString())){
			showToast(R.string.input_standard_original_pwd);
			return;
		}
		//判断格式是否正确
		if(!RegularUtils.regular(RegularUtils.REGEX_PWD, et_newpwd.getText().toString())){
			showToast(R.string.input_standard_new_pwd);
			return;
		}

		if (et_newpwd.getText().toString().equals(et_originalpwd.getText().toString())){
			showToast(getString(R.string.original_pwd_error));
			return;
		}

		String zwgt_data = BaseRequestTool2.getFormatedDateString();
		JSONObject map = new JSONObject();
		try {
			map.put("oldpwd", MD5Utils.getMD5String(et_originalpwd.getText().toString()));
			map.put("newpwd",MD5Utils.getMD5String(et_newpwd.getText().toString()));
		} catch (JSONException e) {
			e.printStackTrace();
		}

		JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);

		mPresenter.loadingData(TaskConstants.TASK_UPDATEPWD, getString(R.string.root_url_test) + getString(R.string.api_user_test) + TaskConstants.TASK_UPDATEPWD,
				Constants.EVENT_DATA, jsonParams, BaseRequestTool2.getHearder(mContext,TaskConstants.TASK_POST,getString(R.string.api_user_test) + TaskConstants.TASK_UPDATEPWD,sharedPreferences.getShareProf(UserInfo.USER_NO)
						, sharedPreferences.getShareProf(UserInfo.TOKENID),zwgt_data), false);
	}


	@Override
	public <T> void initializeViews(String task_name, T data) {
		//处理请求
		if(TextUtils.equals(TaskConstants.TASK_UPDATEPWD, task_name)){
			showToast(R.string.modify_success);
//			finish();
			//重新登录
			// TODO: 2017/5/17 将之前的数据清空
			//将之前的页面关闭
			if (BaseAppManager.getInstance().getActivity(MineSettingActivity.class) != null){
				BaseAppManager.getInstance().getActivity(MineSettingActivity.class).finish();
			}

			if (BaseAppManager.getInstance().getActivity(PasswordMangerActivity.class) != null){
				BaseAppManager.getInstance().getActivity(PasswordMangerActivity.class).finish();
			}

			sharedPreferences.putBoolean(UserInfo.ISLOGIN, false);
			sharedPreferences.clear(mContext);

			EventBus.getDefault().post(new EventCenter(Constants.EVENT_DATA_LOGIN_EXPIRED2));//修改密码成功跳转到第一页然后登陆

			readyGoThenKill(LoginActivity.class);
		}
	}

	@Override
	public <T> void initializeError(String task_name, T data) {
		
	}


}
